package cn.com.yiqi.wsc.portal.controller.point;

import cn.com.yiqi.wsc.Ro.PortalPointVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.order.criteria.OrderCriteria;
import cn.com.yiqi.wsc.dao.point.criteria.PointCriteria;
import cn.com.yiqi.wsc.entity.check.Check;
import cn.com.yiqi.wsc.entity.order.OrderMainDTO;
import cn.com.yiqi.wsc.enumeration.CheckTypeEnum;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.outputExcel.OutPutService;
import cn.com.yiqi.wsc.service.point.PortalPointService;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 何刚
 * @version V1.0
 * @Title: PointController
 * @Package cn.com.yiqi.wsc.portal.controller.point
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/3/23
 */
@RequestMapping("/point")
@Controller
public class PointController extends BaseController {

    @Autowired
    private PortalPointService portalPointService;
    @Autowired
    private OutPutService outPutService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("cash/point/index");
        this.getUserData(mv);
        return mv;
    }

    /**
     * 分页查询
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    Result pageList(@RequestBody PointCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        PageInfo pageInfo = portalPointService.pageList(criteria).getReturnValue();

        result.putData("list", pageInfo.getList());
        result.putData("total", pageInfo.getTotal());

        return result;
    }

    /**
     * 审核方法
     *
     * @param check
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    Result check(@RequestBody Check check) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        boolean paramErr = check.getContentType() == null || CheckTypeEnum.POINT_FETCH.getValue() != check.getContentType();

        if (paramErr) {
            return ResultUtil.paramError(result, "参数错误");
        }

        result = portalPointService.check(check);
        return result;

    }

    @RequestMapping("/outPut")
    public void outPut(@RequestBody PointCriteria criteria, HttpServletRequest request, HttpServletResponse response) {
        criteria.setPageSize(500000);
        List<PortalPointVO> list = portalPointService.pageList(criteria).getReturnValue().getList();
        String title = "积分提现";
        String[] hearders = new String[]{"姓名", "申请积分", "申请时间", "状态"};//表头数组
        String[] fields = new String[]{"customerName", "point", "applyTime", "statusName",};//People对象属性数组
        String creater = userService.getCurrentUser().getRealName();
        try {
            outPutService.outPutExcel(list, title, hearders, fields, creater, request, response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
